package com.feinno.extas.app.util;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * 使用Caffeine设置本地缓存
 */
public class LocalCacheUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(LocalCacheUtil.class);
    private static LoadingCache<String, String> loadingCache;
    /**
     * 缓存最大条数
     */
    private static final int MAXIMUM_SIZE = 3000;
    /**
     * 缓存最长时间
     */
    private static final int MAXIMUM_TIME = 30;

    static {
        loadingCache = Caffeine.newBuilder()
                .maximumSize(MAXIMUM_SIZE)
                .expireAfterWrite(MAXIMUM_TIME, TimeUnit.MINUTES)
                .build(key -> null);
        LOGGER.info("Local Cache Init");
    }

    public static String getString(String key) {
        return loadingCache.get(key);
    }

    public static void setString(String key, String value) {
        loadingCache.put(key, value);
    }
}
